from bs4 import BeautifulSoup
import re
import base64
import requests
from io import BytesIO

def downloadPage(link,file):
    res = requests.get(link)
    res.encoding = "utf-8"
    html = res.text
    
    soup = BeautifulSoup(html, 'html.parser')
    [s.extract() for s in soup('script')] #移去script
    #处理图片
    for img in soup.find_all('img',attrs={'data-src': re.compile("(http)|()")}):
        b = base64.b64encode(BytesIO(requests.get(img.get('data-src')).content).read()).decode()
        img['src'] = 'data:image/' + img.get('data-type') + ';base64,' + b
    
    #f_htmlPath = soup.title.string.rstrip() + '.html'
    f = open(file,'wb')
    f.write(soup.encode('utf-8'))
    f.close()
